package webTest.sessionCookie.Session;


import javax.servlet.ServletException;
import javax.servlet.http.*;

import java.io.IOException;

/**
 * Created by IFT8 on 2015/1/29.
 */
public class GetSomething extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");//设置为UTF-8编码
        HttpSession session = request.getSession();
        String id = request.getParameter("id");
        if (GoodsDAO.getGoods(id) != null) {
            session.setAttribute("id", id);
        }
        /*设置同名Cookie覆盖，以保证时限*/
        int time = 60 * 30;
        Cookie cookie = new Cookie("JSESSIONID", request.getSession().getId());
        cookie.setPath(request.getContextPath());
        cookie.setMaxAge(time);
        response.addCookie(cookie);
        //同时设置服务器Session生命周期
        request.getSession().setMaxInactiveInterval(time);

        /* encodeRedirectURL 如果用户禁用Cookie在url添加SessionID信息 */
        response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "Pay"));
    }
}
